//
//  KUserAuthenticationPhotoCell.m
//  Keping
//
//  Created by Joanlove on 2017/12/21.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KUserAuthenticationPhotoCell.h"
#import <TZImagePickerController/TZImageManager.h>
#import <SDWebImage/UIButton+WebCache.h>

@implementation KUserAuthenticationPhotoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.front.layer.masksToBounds = YES;
    self.front.layer.cornerRadius = 10.0;
    self.front.layer.borderWidth = .5;
    self.front.layer.borderColor = RGBCOLOR(190, 190, 190).CGColor;
    
    self.back.layer.masksToBounds = YES;
    self.back.layer.cornerRadius = 10.0;
    self.back.layer.borderWidth = .5;
    self.back.layer.borderColor = RGBCOLOR(190, 190, 190).CGColor;
}

- (IBAction)actionForPhoto:(id)sender {
    UIButton *button = sender;
    if (self.actionForPhotoAlum) {
        self.actionForPhotoAlum(button.tag);
    }
}

- (void)setUserAuthenticationDict:(NSDictionary *)userAuthenticationDict {
    WEAKSELF
    if ([userAuthenticationDict objectForKey:@"icPhotoFront"]) {
        id obj = [userAuthenticationDict objectForKey:@"icPhotoFront"];
        if ([obj isKindOfClass:[NSArray class]]) {
            TZAssetModel *model = [[userAuthenticationDict objectForKey:@"icPhotoFront"] firstObject];
            [[TZImageManager alloc] getOriginalPhotoWithAsset:model completion:^(UIImage *photo, NSDictionary *info) {
                BOOL isInfo = info[PHImageResultIsDegradedKey];
                if (isInfo) {
                    [weakSelf.front setImage:photo forState:UIControlStateNormal];
                }
            }];
        } else {
            [self.front sd_setImageWithURL:[NSURL URLWithString:[KGetImageURL stringByAppendingString:obj]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"addImage"]];
        }
    }
    
    if ([userAuthenticationDict objectForKey:@"icPhotoBack"]) {
        id obj = [userAuthenticationDict objectForKey:@"icPhotoBack"];
        if ([obj isKindOfClass:[NSArray class]]) {
            TZAssetModel *model = [[userAuthenticationDict objectForKey:@"icPhotoBack"] firstObject];
            [[TZImageManager alloc] getOriginalPhotoWithAsset:model completion:^(UIImage *photo, NSDictionary *info) {
                BOOL isInfo = info[PHImageResultIsDegradedKey];
                if (isInfo) {
                     [weakSelf.back setImage:photo forState:UIControlStateNormal];
                }
            }];
        } else {
            [self.back sd_setImageWithURL:[NSURL URLWithString:[KGetImageURL stringByAppendingString:obj]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"addImage"]];
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
